package com.loongtech.bi.action.game;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.game.InstanceGameManager;
import com.loongtech.bi.manager.game.InstanceGameManager.InstancePlayRadio;
import com.loongtech.bi.manager.game.InstanceGameManager.InstanceStayActiveRadio;
import com.loongtech.bi.manager.game.InstanceGameManager.InstanceStayLossRadio;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.*;

import static com.loongtech.bi.manager.system.GameParamConfigManager.ALL_FLAT;
import static com.loongtech.bi.manager.system.GameParamConfigManager.ALL_FLAT_INT;

/**
 * 游戏内分析-副本分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class InstanceGameAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private int queryInstanceType = ALL_FLAT_INT;  // 前端传过来的查询条件：副本类型
	private int queryInstanceName = ALL_FLAT_INT;  // 前端传过来的查询条件：副本名称
	
	private Map<String, String> instanceTypeItmes = GameParamConfigManager.getItems(CommonGameParam.INSTANCE_TYPE); // 副本类型
	private Map<String, String> instanceTypeItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.INSTANCE_TYPE);

	private Map<String, String> instanceNameItmes = GameParamConfigManager.getItems(CommonGameParam.INSTANCE_NAME); // 副本名称
	private Map<String, String> instanceNameItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.INSTANCE_NAME);
    private Map<String, String> uiInstanceNameItmes = getUIInstanceName();
	
	private List<InstancePlayRadio> instancePlayRadios; // 参与率查询结果
	private List<InstanceStayActiveRadio> instanceStayActiveRadios; // 活跃用户停留率查询结果
	private List<InstanceStayLossRadio> instanceStayLossRadios; // 新增次日流失用户停留率查询结果
	
	private List<InstanceStayActiveRadio> instanceStayActiveRadiosTmp; // 活跃用户停留率图表辅助-取instanceStayActiveRadios中dateEnd天的
	private List<InstanceStayLossRadio> instanceStayLossRadiosTmp; // 新增次日流失用户停留率图表辅助-取instanceStayLossRadios中dateEnd天的
	private ChartUIData instanceStayActiveRadioChart; // 活跃用户停留率图表
	private ChartUIData instanceStayLossRadiosChart; // 新增次日流失用户停留率图表
	
	public static Map<String, String> instanceOpenLevel = GameParamConfigManager.getItemsRevise(CommonGameParam.INSTANCE_OPEN_LEVEL);  // 配置的副本开启等级
	public static Map<String, String> instanceName = GameParamConfigManager.getItemsRevise(CommonGameParam.INSTANCE_NAME);  // 配置的副本名称
	public static Map<String, String> instanceType = GameParamConfigManager.getItemsRevise(CommonGameParam.INSTANCE_TYPE);  // 配置的副本类型
	public static String instance_join_status = GameParamConfigManager.getItemsRevise(CommonGameParam.INSTANCE_JOIN_STATUS).keySet().toArray()[1].toString();  // 副本参与状态ID
	public static String instance_pass_status = GameParamConfigManager.getItemsRevise(CommonGameParam.INSTANCE_PASS_STATUS).keySet().toArray()[1].toString();  // 副本完成状态ID
	
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	@Autowired
	private InstanceGameManager instanceGameManager;
	
	@PostConstruct
	public void init() {
		/*instanceTypeItmes = new LinkedHashMap<>();
		instanceTypeItmes.put("全部副本类型", "0");
		instanceTypeItmes.put("类型1", "1");
		
		instanceNameItmes = new LinkedHashMap<>();
		instanceNameItmes.put("全部副本名称", "0");
		instanceNameItmes.put("名称1", "1");*/
		
		search();
	}

	public void search() {
		sysLogManager.addLog(session.getUserName(), "游戏内分析-副本分析");
		FacesContext context = FacesContext.getCurrentInstance();
		if (!GameParamConfigManager.isConfiged(CommonGameParam.INSTANCE_OPEN_LEVEL)) {
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "没有配置副本开启等级"));
			return;
		}
		if (!GameParamConfigManager.isConfiged(CommonGameParam.INSTANCE_NAME)) {
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "没有配置副本名称"));
			return;
		}
		if (!GameParamConfigManager.isConfiged(CommonGameParam.INSTANCE_TYPE)) {
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "没有配置副本类型"));
			return;
		}
		if (!GameParamConfigManager.isConfiged(CommonGameParam.INSTANCE_JOIN_STATUS)) {
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "副本参与状态ID"));
			return;
		}
		if (!GameParamConfigManager.isConfiged(CommonGameParam.INSTANCE_PASS_STATUS)) {
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "副本通过状态ID"));
			return;
		}

        List<Integer> selectedInstanceName = getSelectedInstanceName();
        Map<String, Map<String, Integer>> levelNumTemp = instanceGameManager.getAllUserByLevel(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		instancePlayRadios = instanceGameManager.getInstancePlayRadios(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, selectedInstanceName, levelNumTemp);
		instanceStayActiveRadios = instanceGameManager.getInstanceStayActiveRadios(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, selectedInstanceName, levelNumTemp);
		instanceStayLossRadios = instanceGameManager.getInstanceStayLossRadios(dateStart, dateEnd, queryPlatform, queryChannel, queryServer, selectedInstanceName, levelNumTemp);
		
		if (instancePlayRadios != null && instancePlayRadios.size() > 0) {
			Collections.sort(instancePlayRadios, instanceGameManager.new InstancePlayRadio().comparatorDate);
		}
		
		if (instanceStayActiveRadios != null && instanceStayActiveRadios.size() > 0) {
			Collections.sort(instanceStayActiveRadios, instanceGameManager.new InstanceStayActiveRadio().comparatorDate);
			
			instanceStayActiveRadiosTmp = new ArrayList<>();
            instanceStayActiveRadiosTmp.add(instanceStayActiveRadios.get(0));
            String tmpDateStr = instanceStayActiveRadios.get(0).getDateStr();
            for (int i = 1; i < instanceStayActiveRadios.size(); i++) {
                if (tmpDateStr.equals(instanceStayActiveRadios.get(i).getDateStr()) && instanceStayActiveRadiosTmp.size() < 15) {
                    instanceStayActiveRadiosTmp.add(instanceStayActiveRadios.get(i));
                } else {
                    break;
                }
            }

            Collections.sort(instanceStayActiveRadiosTmp, instanceGameManager.new InstanceStayActiveRadio().comparatorRadio);

			instanceStayActiveRadioChart = new ChartUIData();
			instanceStayActiveRadioChart.setJsonStr(new ChartTool(instanceStayActiveRadiosTmp, InstanceStayActiveRadio.class, "").loadData(ChartType.COLUMN.type));
		}
		
		if (instanceStayLossRadios != null && instanceStayLossRadios.size() > 0) {
			Collections.sort(instanceStayLossRadios, instanceGameManager.new InstanceStayLossRadio().comparatorDate);
			
			instanceStayLossRadiosTmp = new ArrayList<>();
            instanceStayLossRadiosTmp.add(instanceStayLossRadios.get(0));
            String tmpDateStr = instanceStayLossRadios.get(0).getDateStr();
            for (int i = 1; i < instanceStayLossRadios.size(); i++) {
                if (tmpDateStr.equals(instanceStayLossRadios.get(i).getDateStr()) && instanceStayLossRadiosTmp.size() < 15) {
                    instanceStayLossRadiosTmp.add(instanceStayLossRadios.get(i));
                } else {
                    break;
                }
            }

            Collections.sort(instanceStayLossRadiosTmp, instanceGameManager.new InstanceStayLossRadio().comparatorRadio);

			instanceStayLossRadiosChart = new ChartUIData();
			instanceStayLossRadiosChart.setJsonStr(new ChartTool(instanceStayLossRadiosTmp, InstanceStayLossRadio.class, "").loadData(ChartType.COLUMN.type));
		}
	}

    private List<Integer> getSelectedInstanceName() {
        List<Integer> result = new ArrayList<>();
        if (queryInstanceName == ALL_FLAT_INT) {
            if (queryInstanceType == ALL_FLAT_INT) {
                for (Map.Entry<String, String> entry : instanceTypeItmesRevise.entrySet()) {
                    result.add(Integer.parseInt(entry.getKey()));
                }
            } else {
                String typeName = getInstanceType(queryInstanceType);
                for (Map.Entry<String, String> entry : instanceTypeItmesRevise.entrySet()) {
                    if (entry.getValue().equals(typeName)) {
                        result.add(Integer.parseInt(entry.getKey()));
                    }
                }
            }
        } else {
            result.add(queryInstanceName);
        }

        return result;
    }

    private Map<String, String> getUIInstanceName() {
        Map<String, String> result = new LinkedHashMap<>();
        if (queryInstanceType == ALL_FLAT_INT) {
            result = instanceNameItmes;
        } else {
            result.put("全部副本名称", ALL_FLAT);

            String typeName = getInstanceType(queryInstanceType);
            for (Map.Entry<String, String> entry : instanceTypeItmesRevise.entrySet()) {
                if (entry.getValue().equals(typeName)) {
                    result.put(getInstanceName(entry.getKey()), entry.getKey());
                }
            }
        }

        return result;
    }

    public void selectInstanceType() {
        uiInstanceNameItmes = getUIInstanceName();
        queryInstanceName = ALL_FLAT_INT;
    }

    public void selectInstanceName() {
        String typeName = getInstanceType(queryInstanceName);
        for (Map.Entry<String, String> entry : instanceTypeItmes.entrySet()) {
            if (entry.getKey().equals(typeName)) {
                queryInstanceType = Integer.parseInt(entry.getValue());
                break;
            }
        }
    }

	public String getInstanceType(Object itemType) {
		String key = itemType == null ? "" : Math.abs(Integer.parseInt(itemType.toString())) + "";
		return (instanceTypeItmesRevise.get(key) == null) ? key : instanceTypeItmesRevise.get(key); 
	}
	
	public String getInstanceName(Object itemType) {
		String key = itemType == null ? "" : Math.abs(Integer.parseInt(itemType.toString())) + "";
		return (instanceNameItmesRevise.get(key) == null) ? key : instanceNameItmesRevise.get(key); 
	}

	@Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public int getQueryInstanceType() {
		return queryInstanceType;
	}

	public void setQueryInstanceType(int queryInstanceType) {
		this.queryInstanceType = queryInstanceType;
	}

	public int getQueryInstanceName() {
		return queryInstanceName;
	}

	public void setQueryInstanceName(int queryInstanceName) {
		this.queryInstanceName = queryInstanceName;
	}

	public List<InstancePlayRadio> getInstancePlayRadios() {
		return instancePlayRadios;
	}

	public void setInstancePlayRadios(List<InstancePlayRadio> instancePlayRadios) {
		this.instancePlayRadios = instancePlayRadios;
	}

	public List<InstanceStayActiveRadio> getInstanceStayActiveRadios() {
		return instanceStayActiveRadios;
	}

	public void setInstanceStayActiveRadios(List<InstanceStayActiveRadio> instanceStayActiveRadios) {
		this.instanceStayActiveRadios = instanceStayActiveRadios;
	}

	public List<InstanceStayLossRadio> getInstanceStayLossRadios() {
		return instanceStayLossRadios;
	}

	public void setInstanceStayLossRadios(List<InstanceStayLossRadio> instanceStayLossRadios) {
		this.instanceStayLossRadios = instanceStayLossRadios;
	}

	public List<InstanceStayActiveRadio> getInstanceStayActiveRadiosTmp() {
		return instanceStayActiveRadiosTmp;
	}

	public void setInstanceStayActiveRadiosTmp(
			List<InstanceStayActiveRadio> instanceStayActiveRadiosTmp) {
		this.instanceStayActiveRadiosTmp = instanceStayActiveRadiosTmp;
	}

	public List<InstanceStayLossRadio> getInstanceStayLossRadiosTmp() {
		return instanceStayLossRadiosTmp;
	}

	public void setInstanceStayLossRadiosTmp(
			List<InstanceStayLossRadio> instanceStayLossRadiosTmp) {
		this.instanceStayLossRadiosTmp = instanceStayLossRadiosTmp;
	}

	public ChartUIData getInstanceStayActiveRadioChart() {
		return instanceStayActiveRadioChart;
	}

	public void setInstanceStayActiveRadioChart(
			ChartUIData instanceStayActiveRadioChart) {
		this.instanceStayActiveRadioChart = instanceStayActiveRadioChart;
	}

	public ChartUIData getInstanceStayLossRadiosChart() {
		return instanceStayLossRadiosChart;
	}

	public void setInstanceStayLossRadiosChart(
			ChartUIData instanceStayLossRadiosChart) {
		this.instanceStayLossRadiosChart = instanceStayLossRadiosChart;
	}

	public Map<String, String> getInstanceTypeItmesRevise() {
		return instanceTypeItmesRevise;
	}

	public void setInstanceTypeItmesRevise(Map<String, String> instanceTypeItmesRevise) {
		this.instanceTypeItmesRevise = instanceTypeItmesRevise;
	}

	public Map<String, String> getInstanceTypeItmes() {
		return instanceTypeItmes;
	}

	public void setInstanceTypeItmes(Map<String, String> instanceTypeItmes) {
		this.instanceTypeItmes = instanceTypeItmes;
	}

	public Map<String, String> getInstanceNameItmes() {
		return instanceNameItmes;
	}

	public void setInstanceNameItmes(Map<String, String> instanceNameItmes) {
		this.instanceNameItmes = instanceNameItmes;
	}

	public Map<String, String> getInstanceNameItmesRevise() {
		return instanceNameItmesRevise;
	}

	public void setInstanceNameItmesRevise(Map<String, String> instanceNameItmesRevise) {
		this.instanceNameItmesRevise = instanceNameItmesRevise;
	}

    public Map<String, String> getUiInstanceNameItmes() {
        return uiInstanceNameItmes;
    }

    public void setUiInstanceNameItmes(Map<String, String> uiInstanceNameItmes) {
        this.uiInstanceNameItmes = uiInstanceNameItmes;
    }
}
